#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <left4dhooks>



#define PLUGIN_NAME                   "l4d2_AWP_Knockback_tank"    //这里写插件名字
#define PLUGIN_AUTHOR                 "豆瓣酱な"                         //作者
#define PLUGIN_DESCRIPTION            "AWP枪击飞tank."   //介绍
#define PLUGIN_VERSION                "1.1"
#define PLUGIN_URL                    "https://gitee.com/sweet-and-sour-bombs"  //网站


public Plugin myinfo =
{
    name        = PLUGIN_NAME,
    author      = PLUGIN_AUTHOR,
    description = PLUGIN_DESCRIPTION,
    version     = PLUGIN_VERSION,
    url         = PLUGIN_URL
}


static const float L4D_Z_MULT = 1.6;


int RoundTankNum;


int g_DmgEnable;
float g_fCvarPower, g_fCvarZMult;
ConVar hCvar_float_g_fCvarPower, hCvar_g_fCvarZMult, hCvar_DmgEnable;
bool g_bL4D2Version;

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
    EngineVersion test = GetEngineVersion();

    if( test == Engine_Left4Dead )
    {
        g_bL4D2Version = false;
    }
    else if( test == Engine_Left4Dead2 )
    {
        g_bL4D2Version = true;
    }
    else
    {
        strcopy(error, err_max, "Plugin only supports Left 4 Dead 1 & 2.");
        return APLRes_SilentFailure;
    }

    return APLRes_Success;
}


public void OnPluginStart()
{

	hCvar_DmgEnable = CreateConVar("hcvar_AWP_Knockback_tank", "1", "是否启用插件?", FCVAR_NOTIFY, true, 0.0, true, 1.0);
	hCvar_float_g_fCvarPower = CreateConVar("LCvar_AWP_g_fCvarPower", "250.0", "坦克被AWP枪击飞的距离.", FCVAR_NOTIFY, true, 0.0);
	hCvar_g_fCvarZMult = CreateConVar("LCvar_g_fCvarZAWP", "1.5", "坦克被AWP枪击飞的高度.", FCVAR_NOTIFY, true, 0.0);
	CreateConVar("l4d2_AWP_Knockback_tank_version", PLUGIN_VERSION, "插件版本", FCVAR_NOTIFY);


	HookConVarChange(hCvar_DmgEnable, eConvarChanged);
	HookConVarChange(hCvar_float_g_fCvarPower, eConvarChanged);
	HookConVarChange(hCvar_g_fCvarZMult, eConvarChanged);

		
	HookEvent("round_start", Event_RoundStart, EventHookMode_Pre);  //回合开始事件.
	HookEvent("player_hurt",			Event_PlayerHurt, EventHookMode_Pre);  //玩家受伤事件

	//AutoExecConfig(true, "l4d2_AWP_Knockback_tank");

}




public void OnMapStart()
{
	CvarsChanged();
}

public void eConvarChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
{
	CvarsChanged();
}

void CvarsChanged()
{
	g_DmgEnable = hCvar_DmgEnable.IntValue;
	g_fCvarPower = hCvar_float_g_fCvarPower.FloatValue;
	g_fCvarZMult = hCvar_g_fCvarZMult.FloatValue;

}

//回合开始.
public void Event_RoundStart(Event event, const char[] name, bool dontBroadcast)
{
	RoundTankNum = false;

}





public void Event_PlayerHurt(Event event, const char[] name, bool dontBroadcast)
{
	if(g_DmgEnable == 0)
		return;
		
	char weapon[64];
	int attacker = GetClientOfUserId(event.GetInt("attacker"));
	int victim = GetClientOfUserId(event.GetInt("userid"));
	event.GetString("weapon", weapon, sizeof(weapon));
	if (attacker > 0 && IsClientInGame(attacker) && GetClientTeam(attacker) == 2 && IsPlayerAlive(attacker))
	{
		
		if (StrEqual(weapon, "sniper_awp"))
		{
		
			if(IsTank(victim) && RoundTankNum == 0)
			{
				float HeadingVector[3], resulting[3];
				GetClientEyeAngles(attacker, HeadingVector);
				GetEntPropVector(victim, Prop_Data, "m_vecVelocity", resulting);

				resulting[0] += Cosine(DegToRad(HeadingVector[1])) * g_fCvarPower;
				resulting[1] += Sine(DegToRad(HeadingVector[1])) * g_fCvarPower;
				resulting[2] = g_fCvarPower * g_fCvarZMult;

				if (g_bL4D2Version){
					resulting[2] *= L4D_Z_MULT;
					TeleportEntity(victim, NULL_VECTOR, NULL_VECTOR, resulting);
				}
				else
					L4D2_CTerrorPlayer_Fling(victim, attacker, resulting);
			}
		
			else 
			{
				float fPos[3];
				GetClientAbsOrigin(attacker, fPos);
				L4D_StaggerPlayer(victim, attacker, fPos);
			}
			RoundTankNum = RoundTankNum + 1;
			if(RoundTankNum > 10) RoundTankNum = false;
		}	
	}
		

	
}



bool IsInfected(int client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 3;
}

bool IsTank(int client)  
{
	if (!IsInfected(client))
		return false;

	if (GetEntProp(client, Prop_Send, "m_zombieClass") != 8)
		return false;

	return true;
}
